--- @param extension Package
--- @param HY HY_Utility
--- @param U Utility
return function(extension, HY, U)
    local hy_matara_okina = General:new(extension, "hy_matara_okina", "ten_k", 3, 4, General.Female)
    local backdoor_god = fk.CreateSkill {
        name = "backdoor_god",
    }
    backdoor_god:addEffect(fk.EventPhaseChanging, {
        anim_type = "control",
        can_trigger = function(self, event, target, player, data)
            return player:hasSkill(backdoor_god.name)
        end,
        on_cost = function(self, event, target, player, data)
            return target == player and data.phase == Player.Play
        end,
        on_use = function(self, event, target, player, data)
            target:skip(data.phase)
            data.skipped = true
        end,
    })
    backdoor_god:addEffect(fk.EventPhaseEnd, {
        anim_type = "control",
        can_trigger = function(self, event, target, player, data)
            return player:hasSkill(backdoor_god.name)
        end,
        on_cost = function(self, event, target, player, data)
            if target.phase == Player.Draw then
                if #player.player_cards[Player.Hand] == 0 then
                    return false
                end
                local choices = {
                    "backdoor_god_use",
                    "backdoor_god_damage",
                    "backdoor_god_give",
                    "backdoor_god_recast",
                    "Cancel",
                }
                if target == player then
                    table.removeOne(choices, "backdoor_god_give")
                end
                if #table.filter(player.player_cards[Player.Hand], function(id)
                        return not player:prohibitDiscard(Fk:getCardById(id))
                    end) < player.hp then
                    table.removeOne(choices, "backdoor_god_damage")
                end
                local result = player.room:askToChoice(player, {
                    choices = choices,
                    skill_name = backdoor_god.name,
                    prompt = "backdoor_god_prompt::" .. target.id
                })
                if result == "Cancel" then
                    return false
                end
                event:setCostData(self, result)
                return true
            end
        end,
        on_use = function(self, event, target, player, data)
            local room = player.room
            if event:getCostData(self) == "backdoor_god_use" then
                local i = 0
                while player.hp - i > 0 do
                    if target.dead or #player.player_cards[Player.Hand] == 0 then
                        break
                    end
                    local _, _data = room:askToUseActiveSkill(player, {
                        skill_name = "backdoor_god_active",
                        prompt = "backdoor_god_use_prompt::" .. target.id .. ":" .. tostring(player.hp - i),
                        extra_data = { bypass_distances = true, bypass_times = true }
                    })
                    if _data then
                        local card = Fk:getCardById(_data.cards[1])
                        i = i + 1
                        room:useCard({
                            card = card,
                            from = player,
                            tos = card.name == "collateral" and { target, _data.targets[1] } or { target },
                            extraUse = true,
                        })
                    else
                        break
                    end
                end
            elseif event:getCostData(self) == "backdoor_god_damage" then
                room:askToDiscard(player, {
                    min_num = player.hp,
                    max_num = player.hp,
                    skill_name = backdoor_god.name,
                    cancelable = false,
                    prompt = "backdoor_god_damage_prompt::" .. target.id .. ":" .. tostring(player.hp),
                    no_indicate = true,
                })
                room:damage({
                    from = player,
                    to = target,
                    damage = 1,
                    damageType = fk.NormalDamage,
                    skillName = backdoor_god.name,
                })
            elseif event:getCostData(self) == "backdoor_god_give" then
                local cardIds = room:askToCards(player, {
                    min_num = 1,
                    max_num = player.hp,
                    skill_name = backdoor_god.name,
                    cancelable = false,
                    prompt = "backdoor_god_give_prompt::" .. target.id .. ":" .. tostring(player.hp)
                })
                room:moveCards({
                    from = player,
                    toArea = Card.PlayerHand,
                    to = target,
                    proposer = player,
                    ids = cardIds,
                    skillName = backdoor_god.name,
                    moveReason = fk.ReasonGive,
                    moveVisible = false,
                })
            elseif event:getCostData(self) == "backdoor_god_recast" then
                local cardIds = room:askToCards(player, {
                    min_num = 1,
                    max_num = 1,
                    skill_name = backdoor_god.name,
                    cancelable = false,
                    prompt = "backdoor_god_recast_prompt",
                    no_indicate = true
                })
                room:recastCard(cardIds, player, backdoor_god.name)
            end

            local marks = player.tag["backdoor_god"] or {}
            if not table.contains(marks, event:getCostData(self)) and not player.dead then
                room:drawCards(player, 1, backdoor_god.name)
                table.insertIfNeed(marks, event:getCostData(self))
                player.tag["backdoor_god"] = marks
            end
        end,
    })
    backdoor_god:addEffect("maxcards", {
        fixed_func = function(self, player)
            if player:hasSkill(backdoor_god.name) then
                return player.hp + player.maxHp
            end
        end
    })
    backdoor_god:addEffect(fk.RoundStart, {
        can_refresh = function(self, event, target, player, data)
            return player:hasSkill(backdoor_god.name)
        end,
        on_refresh = function(self, event, target, player, data)
            player.tag["backdoor_god"] = {}
        end,
    })
    local backdoor_god_active = fk.CreateSkill {
        name = "backdoor_god_active",
    }
    backdoor_god_active:addEffect("active", {
        card_filter = function(self, player, to_select, selected)
            if #selected == 0 then
                local card = Fk:getCardById(to_select)
                local room = Fk:currentRoom()
                local target = table.filter(room.alive_players, function(t)
                    return t.phase == Player.Draw
                end)[1]
                return table.contains(player.player_cards[Player.Hand], to_select) and player:canUseTo(card, target, { bypass_distances = true, bypass_times = true })
            end
        end,
        feasible = function(self, player, selected, selected_cards, card)
            if #selected_cards == 0 then
                return false
            end
            local card = Fk:getCardById(selected_cards[1])
            return card.name ~= "collateral" or #selected == 1
        end,
        target_filter = function(self, player, to_select, selected, selected_cards)
            if #selected_cards ~= 1 then
                return false
            end
            local card = Fk:getCardById(selected_cards[1])
            local room = Fk:currentRoom()
            local target = table.filter(room.alive_players, function(t)
                return t.phase == Player.Draw
            end)[1]
            if #selected == 0 and card.name == "collateral" then
                return target:inMyAttackRange(to_select) and not target:isProhibited(to_select, Fk:cloneCard("slash"))
            end
        end,
    })
    extension:loadSkillSkels { backdoor_god, backdoor_god_active }
    hy_matara_okina:addSkill("backdoor_god")
    Fk:loadTranslationTable {
        ["hy_matara_okina"] = "摩多罗隐岐奈",
        ["#hy_matara_okina"] = "幕后现身者",
        ["illustrator:hy_matara_okina"] = "カオリング",
        ["designer:hy_matara_okina"] = "黑曜人形",
        ["cv:hy_matara_okina"] = "",

        ["backdoor_god"] = "后神",
        [":backdoor_god"] = "你跳过出牌阶段且手牌上限为体力值与体力上限之和。一名角色的摸牌阶段结束时，你可以选择一项：1.对其使用至多X张牌（无距离限制）；2.弃置X张手牌并对其造成1点伤害；3.交给其1~X张手牌；4.重铸一张手牌。在你选择的选项于本轮首次执行后，你摸一张牌。（X为你的体力值）",
        ["backdoor_god_prompt"] = "后神：选择对%dest执行的一项",
        ["backdoor_god_use"] = "对其使用牌",
        ["backdoor_god_damage"] = "弃置手牌并造成伤害",
        ["backdoor_god_give"] = "交给其手牌",
        ["backdoor_god_recast"] = "重铸一张手牌",
        ["backdoor_god_use_prompt"] = "后神：还可以对%dest使用%arg张牌",
        ["backdoor_god_damage_prompt"] = "后神：弃置%arg张牌并对%dest造成一点伤害",
        ["backdoor_god_give_prompt"] = "后神：交给%dest 1~%arg张牌",
        ["backdoor_god_recast_prompt"] = "后神：重铸一张手牌",

        ["backdoor_god_active"] = "后神",
    }
end
